<?php

// +----------------------------------------------------------------------
// | TAODUDU.COM  [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.taodudu.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: ubten <462193409@qq.com> http://www.ubten.com/
// +----------------------------------------------------------------------

namespace User\Controller;
use Think\Controller;

class PublicController extends Controller {

    /**
     * 功能：聚合手机短信通过Ajax发送验证码
     * @param 
     * @return 返回图片短信发送的状态
     */
    public function sent_mobile_yzm() {
        $k = I('k');
        $mobile = I('mobile');
        if (empty($mobile)) {
            $ajax = array(
                'msg' => '手机号不能为空',
                'boo' => false,
            );
            echo json_encode($ajax);
            return FALSE;
        }
        
        if (!$this->check_verify($k)) {
            $ajax = array(
                'msg' => '验证码不正确',
                'boo' => false,
            );
            echo json_encode($ajax);
            return FALSE;
        }

        if (!empty($k)) {
            $t = session('moble_yzm_time');
            if (!empty($t)) {
                $n = NOW_TIME - $t;
                if ($n < 120) {
                    $ajax = array(
                        'msg' => '发送验证码的间隔不能小于120秒',
                        'boo' => false,
                    );
                    echo json_encode($ajax);
                    return FALSE;
                } else {
                    session('moble_yzm_time', null);
                }
            } else {
                session('moble_yzm_time', NOW_TIME);
            }
        } else {
            $ajax = array(
                'msg' => '请输入验证码！',
                'boo' => false,
            );
            echo json_encode($ajax);
            return FALSE;
        }

        $tpl_value = rand(100000, 999999);
        $code_value = '#code#=' . $tpl_value;
        if (!preg_match("/^1[34578]\d{9}$/", $mobile)) {
            $ajax = array(
                'msg' => '您输入的手机号不合法',
                'boo' => true,
            );
            echo json_encode($ajax);
            return FALSE;
        }

        $sms = new \Libs\JuHeSms();
        $code = $sms->sendMobile($mobile, 32216, $code_value);
        session(array('name'=>'mobile_sms','expire'=>800));
        session('mobile_sms', $tpl_value);
        $code['code'] = 1;
        if ($code['code'] == 1) {
            $ajax = array(
                'msg' => '发送成功',
                'boo' => true,
            );
            echo json_encode($ajax);
            return TRUE;
        } else {
            $ajax = array(
                'msg' => '信息请求失败',
                'boo' => true,
            );
            echo json_encode($ajax);
            return FALSE;
        }
    }

    /**
     * 功能：通过Ajax发送邮箱验证码
     * @param 
     * @return 返回图片短信发送的状态
     */
    public function sent_email_yzm() {
        $email = I('email');
        $re = sendMail($email,1);
        if($re){
            $ajax = array(
                'msg' => '发送成功,注意查收邮件！',
                'boo' => true,
            );
            echo json_encode($ajax);
            return TRUE;
        }else{
            $ajax = array(
                'msg' => '信息请求失败',
                'boo' => true,
            );
            echo json_encode($ajax);
            return FALSE;
        }
    }
    
    //判断手机号是否注册
    public function phone_verification() {
        $map['user_mobile'] = I('param');
        $m = M('Users')->where($map)->find();
        if ($m) {
            $this->ajaxReturn(array('info' => '手机号已被占用', 'status' => 'n'));
        }
        $this->ajaxReturn(array('info' => '验证通过', 'status' => 'y'));
    }

    //判断手机号是否注册
    public function email_verification() {
        $map['user_email'] = I('param');
        $m = M('Users')->where($map)->find();
        if ($m) {
            $this->ajaxReturn(array('info' => '邮箱已被占用', 'status' => 'n'));
        }
        $this->ajaxReturn(array('info' => '验证通过', 'status' => 'y'));
    }
    

        // 生成验证码
    public function verify(){
        $config =    array(
            'fontSize'    =>    24,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'imageW'      =>    0,     // 验证码宽度
            'imageH'      =>    0,     // 验证码高度
            'fontttf'     =>   '1.ttf',
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 验证验证码
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }

}
